package com.padakeji.android.pworks.module.image.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiConfiguration;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.padakeji.android.pworks.R;
import com.padakeji.android.pworks.content.PadaImageLoader;
import com.padakeji.android.pworks.content.PadaImageLoaderFactory;
import com.padakeji.android.pworks.content.UriProtocol;
import com.padakeji.android.pworks.ui.UIHelper;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by AtanL on 2015/9/20.
 */
public class ImagePagerAdapter extends PagerAdapter {

    private class ViewHolder {
        public ImageView itemImage;
    }

    private Context mContext;
    private LayoutInflater mInflater;
    private PadaImageLoader imageLoader;
    private List<String> imagePaths;
    private List<View> pages;

    public ImagePagerAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        imageLoader = PadaImageLoaderFactory.getImageLoader(mContext);
        imagePaths = new ArrayList<>();
        initPages();
    }

    private void initPages() {
        pages = new ArrayList<>();
        if (imagePaths != null) {
            int i, count = imagePaths.size();
            for (i = 0 ; i < count; ++i) {
                ViewHolder vh = new ViewHolder();
                View v = mInflater.inflate(R.layout.pworks_fragment_image_viewer_item_image, null, false);
                vh.itemImage = (ImageView)v.findViewById(R.id.pworks_fragment_image_viewer_image_item);
                v.setTag(vh);
                pages.add(v);
            }
        }
    }


    /**
     * 该方法会重置之前的数据。
     * @param imagePaths
     */
    public void setImagePaths(List<String> imagePaths) {
        if (imagePaths == null) {
            imagePaths = new ArrayList<>();
        }
        this.imagePaths.clear();
        this.imagePaths.addAll(imagePaths);
        initPages();
        notifyDataSetChanged();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = pages.get(position);
        ViewHolder vh = (ViewHolder)v.getTag();
        String uri = UriProtocol.FILE + imagePaths.get(position);
        imageLoader.display(uri, vh.itemImage);
        container.addView(v);
        return v;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View v = (View) object;
        ViewHolder vh = (ViewHolder)v.getTag();
        vh.itemImage.setImageDrawable(null);
        container.removeView(v);
    }

    @Override
    public int getCount() {
        return pages.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
